# 大单买卖类
import sys

sys.path.append(r"/data/finance.Source/utils")
sys.path.append(r"/data/finance.Source/Analysis/static")

import sqlutils
import codes
import datetimeutils 

class Bigbs():
    def __init__(self,codeObject,dNum,startdate):
        self.code = codeObject['code']
        self.cname = codeObject['name']
        self.startdate = startdate
        self.dNum = dNum  # 计算天数
        self.bigNum = codeObject['bigNum']
        self.numthen = 1.0  # (多开+空平) : (空开+多平)
        self.numdiffer = ''   # (多开+空平) - (空开+多平)
        self.bigtype = 'tie'  # tie 平局 buy 买 sell 卖
        self.score = 0   # 100 绝对多 -100 绝对空
        self.OpenBuyCloseSellNum = 0,
        self.OpenSellCloseBuyNum = 0,
        self.OpenBuyNum = 0
        self.OpenSellNum = 0  # 空开总数
        self.CloseBuyNum = 0  # 多平总数
        self.CloseSellNum = 0 # 空平总数
        self.OpenBuyHighetPrice = 0
        self.OpenBuyLowestPrice = 1000000
        self.OpenSellHighetPrice = 0
        self.OpenSellLowestPrice = 1000000
        self.CloseBuyHighetPrice = 0
        self.CloseBuyLowestPrice = 1000000
        self.CloseSellHighetPrice = 0
        self.CloseSellLowestPrice = 1000000
        self.OpenBuyTotalPrice = 0.0
        self.OpenSellTotalPrice =  0.0
        self.CloseBuyTotalPrice = 0.0
        self.CloseSellTotalPrice = 0.0
        
    def countNum(self,line):
#        print(line)
        #    print(line['Type'])
        if line['Type'] == '多开':
            self.OpenBuyNum+= line['OpenVol']
            self.OpenBuyTotalPrice+= line['NewPrice'] * line['OpenVol']/10000
            if line['NewPrice'] > self.OpenBuyHighetPrice:
                self.OpenBuyHighetPrice= line['NewPrice']
            if line['NewPrice'] < self.OpenBuyLowestPrice:
            #            print('set OpenbuyLowestprice')
                self.OpenBuyLowestPrice= line['NewPrice']
        if line['Type'] == '多平':        
            self.CloseBuyNum+= line['CloseVol']
            self.CloseBuyTotalPrice+= line['NewPrice'] * line['CloseVol']/10000
            if line['NewPrice'] < self.CloseBuyLowestPrice:
                self.CloseBuyLowestPrice= line['NewPrice']
            if line['NewPrice'] > self.CloseBuyHighetPrice:
                self.CloseBuyHighetPrice= line['NewPrice']
        if line['Type'] == '空开':
            self.OpenSellNum += line['OpenVol']
            self.OpenSellTotalPrice+= line['NewPrice'] * line['OpenVol']/10000
            if line['NewPrice'] > self.OpenSellHighetPrice:
                self.OpenSellHighetPrice= line['NewPrice']
            if line['NewPrice'] < self.OpenSellLowestPrice:
                self.OpenSellLowestPrice= line['NewPrice']
        if line['Type'] == '空平':
            self.CloseSellNum+= line['CloseVol']
            self.CloseSellTotalPrice+= line['NewPrice'] * line['CloseVol']/10000
            if line['NewPrice'] < self.CloseSellLowestPrice:
                self.CloseSellLowestPrice= line['NewPrice']
            if line['NewPrice'] > self.CloseSellHighetPrice:
                self.CloseSellHighetPrice= line['NewPrice']

    def ergodicData(self,results):
        for line in results:
            self.countNum(line)

    # 总分比
    def summary(self):
        self.OpenBuyCloseSellNum = self.OpenBuyNum + self.CloseSellNum
        self.OpenSellCloseBuyNum = self.OpenSellNum + self.CloseBuyNum
        self.numdiffer = self.OpenBuyCloseSellNum - self.OpenSellCloseBuyNum
        if self.numdiffer > 0:
            self.bigtype = 'buy'
        elif self.numdiffer < 0:
            self.bigtype = 'selll'
        if self.OpenSellNum + self.CloseBuyNum != 0:
            self.numthen =  (self.OpenBuyNum + self.CloseSellNum)/ (self.OpenSellNum + self.CloseBuyNum)
        elif self.OpenSellNum + self.CloseBuyNum == 0:
            print('空开+多平0手')
            print('空开+多平0手')
            print('空开+多平0手')
            print('空开+多平0手')
        if self.OpenBuyNum + self.CloseSellNum == 0:
            print('多开+空平0手')
            print('多开+空平0手')
            print('多开+空平0手')
            print('多开+空平0手')
 
        
    # 设置总分数
    def setScore(self,zb, allNum):
        print('得分计算 参数:', zb,self.dNum,allNum,self.numdiffer)
        self.score = (self.numdiffer/allNum)/(zb*(abs(self.dNum)-0.2*(abs(self.dNum)-1)))*100
        
    # 打印需要的数据
    def printData(self):
        scorelist = []
        print('超过',self.bigNum, ' 类型:' , self.bigtype)
        print('得分:', self.score)
        print('开多+平空总和: ', self.OpenBuyCloseSellNum)
        print('开空+平多总和: ', self.OpenSellCloseBuyNum)
        print('大单差: ', self.numdiffer)
        print('大单比: ', self.numthen)
        print('多开总数', self.OpenBuyNum)
        scorelist.append('超过'+ str(self.bigNum) + ' 类型:' + self.bigtype)
        scorelist.append('得分:'+ str(self.score))
        scorelist.append('开多+平空总和: '+ str(self.OpenBuyCloseSellNum))
        scorelist.append('开空+平多总和: ' + str(self.OpenSellCloseBuyNum))
        scorelist.append('大单差: '+  str(self.numdiffer))
        scorelist.append('大单比: '+  str(self.numthen))
        scorelist.append('多开总数'+  str(self.OpenBuyNum))
        if self.OpenBuyNum == 0:
            print(self.bigNum,"手,开多为 0")
            print(self.bigNum,"手,开多为 0")
            print(self.bigNum,"手,开多为 0")
            scorelist.append( str(self.bigNum) + "手,开多为 0")
            scorelist.append( str(self.bigNum) + "手,开多为 0")
            scorelist.append( str(self.bigNum) + "手,开多为 0")
        else:
            print('多开最低价',self.OpenBuyLowestPrice,'多开最高价',self.OpenBuyHighetPrice,'多开平均价',self.OpenBuyTotalPrice/(self.OpenBuyNum/10000))
            scorelist.append('多开最低价' +  str(self.OpenBuyLowestPrice) + '多开最高价'+ str(self.OpenBuyHighetPrice) + '多开平均价' +  str(self.OpenBuyTotalPrice/(self.OpenBuyNum/10000)))
        print('空平总数', self.CloseSellNum)
        scorelist.append( '空平总数'+ str(self.CloseSellNum))
        if self.CloseSellNum == 0:
            print(self.bigNum,"手,平空为 0")
            print(self.bigNum,"手,平空为 0")
            print(self.bigNum,"手,平空为 0")
            scorelist.append(str(self.bigNum) + "手,平空为 0")
            scorelist.append(str(self.bigNum) + "手,平空为 0")
            scorelist.append(str(self.bigNum) + "手,平空为 0")
        else:
            print('空平最高价',self.CloseSellHighetPrice, '空平最低价',self.CloseSellLowestPrice,'空平平均价',self.CloseSellTotalPrice/(self.CloseSellNum/10000))
            scorelist.append('空平最高价' + str(self.CloseSellHighetPrice) + '空平最低价' + str(self.CloseSellLowestPrice) + '空平平均价' + str(self.CloseSellTotalPrice/(self.CloseSellNum/10000)))
        print('空开总数', self.OpenSellNum)
        scorelist.append('空开总数'+  str(self.OpenSellNum))
        if self.OpenSellNum == 0:
            print(self.bigNum,"手,空开为 0")
            print(self.bigNum,"手,空开为 0")
            print(self.bigNum,"手,空开为 0")
            scorelist.append(str(self.bigNum) + "手,空开为 0")
            scorelist.append(str(self.bigNum) + "手,空开为 0")
            scorelist.append(str(self.bigNum) + "手,空开为 0")
        else:
            print('空开最高价',self.OpenSellHighetPrice, '空开最低价',self.OpenSellLowestPrice,'多开平均价',self.OpenSellTotalPrice/(self.OpenSellNum/10000))
            scorelist.append('空开最高价' + str(self.OpenSellHighetPrice) + '空开最低价' + str(self.OpenSellLowestPrice) + '多开平均价' + str(self.OpenSellTotalPrice/(self.OpenSellNum/10000)))
        print('多平总数', self.CloseBuyNum)
        scorelist.append('多平总数' + str(self.CloseBuyNum))
        if self.CloseBuyNum == 0:
            print(self.bigNum,"手,多平为 0")
            print(self.bigNum,"手,多平为 0")
            print(self.bigNum,"手,多平为 0")
            scorelist.append(str(self.bigNum) + "手,多平为 0")
            scorelist.append(str(self.bigNum) + "手,多平为 0")
            scorelist.append(str(self.bigNum) + "手,多平为 0")
        else:
            print('多平最高价',self.CloseBuyHighetPrice,'多平最低价',self.CloseBuyLowestPrice,'多平平均价',self.CloseBuyTotalPrice/(self.CloseBuyNum/10000))
            scorelist.append('多平最高价' + str(self.CloseBuyHighetPrice) + '多平最低价' + str(self.CloseBuyLowestPrice) + '多平平均价' + str(self.CloseBuyTotalPrice/(self.CloseBuyNum/10000)))

        return scorelist

#nbigbs = Bigbs({'code':'TA905','name':'PAT201905'})
#print(nbigbs.cname)
